removed debug stuff, added more error handling