Solved null ptr exception. This solution should be a lot safer then the thing I had before.