Revisied version, working but buggy