Changed for readability and bugfix
Added more informative error msgs