code refactor / bug fixing