Fixes and refactored a bit