Changed implementation

More effective and simpler implementation