Added logging and fixed bugs