added more functional code (still a bit buggy though!)