remove code duplication
added test and 1st version of energy method