correctly working code for atomic clusters, heavy optimization needed though