Fixes and refactor