Code refactor minor bug fixes