Bug fixing
A bit of refactoring