Code cleanup, improved performance