Added thread caps to the scheduler. Thread switch working. Other fixes.