Some fixes here and there. Refactoring the code.