Enhancements

Refactored some code and added fixes for #41 and #62.