Added Out Of Memory handling and fixed NPE