Fix #45 and clean up copy/paste code a bit