Code cleanup and refact