refactored and fix lots of computational errors