implemented Samys code, doubled speed