V2.1 with improvements, fixes and custom error function