cleaned up and improved efficiency