Bug fix and code formatting