Refactor and clean up code format