Added null checking

Changed resource type
Reformatted some code