Added helper functions
Added safety and cleaned stuff up