Fixed code to keep from high CPU usage