Version 0.1b. Last commit before thesis submit. This code is in Appendix