fixed logging, moved some error handling