Bug fix and clean code