Main source code; tested.