Refactor millisecond formatting logic with debugging