Many improvements, credits to Toal