code style and edge case bug fix