Refactoring & bug fixing