Code refactoring

- use code contracts
- Functions classes renamed to NativeMethods
- small other things