removed infinite cycle, minor bug fixes