Code refactor/improvements