Bug fix and function layout