Refactoring + fix, seems to be working now :)