Completed implementation (untested)