Code cleanup and improved reuse