Refactors, bug fixes