Added waterproof version, moved things around slightly to ensure things are only executed on the correct side.