Optimized update of Jeremy Keeshin's original