Code cleanup and abstaction