Fixes, refactoring the code