Rework atomic implementation

Use atomic_t instead of a naked int