Added some extra logging and fixed a bug