Optimized hamiltonian construction. Few more small tweaks and tests still needed.