Fixed minor bugs & code refactoring