Refactor code and types