Cleaned up and added a few checks