completely rewritten, faster execution