Added more stable id generation algorithm

Fixed bugs in previous commit