Some improvements suggested by Ryley (atomicint_)