Bug fixes

This pull request has the changes from the refactoring as well as the new bug fixes