Refactor code a bit