added custom error + major refactoring