A new algorithm for calculating the Hamiltonian matrix elements with all-electron explicitly correlated Gaussian functions for quantum-mechanical calculations of atoms with arbitrary angular momentum is presented. The calculations are checked on several excited states of three and four electron systems. The presented formalism can be used as unified framework for high accuracy calculations of properties of small atoms and molecules.