First (seemingly) working implementation