Code cleanup and security enhancements