Added overflow detection and wrapping.