Fixed bug that caused undefined behavior