refactoring complete ... need to fix NaN in JS now