Refactored to conform to code standards, adding missing features, work better