Added logging, fixed bugs