Refactoring and optimizations
closes #27, closes #28