Buggy and ugly code