cleaned up flags and added better error handling