fixing math error and other enhancements