Fixed NPE and / by 0 bugs