Cleaned up logging and added more checks