Completely rewritten code