Code cleanup and a few better solutions