add gitignore
complete rewrite

Main changes:
- Make code compatible with most C standards.
- Improve code readability and logic.
- Split long functions and create some helper functions.
- Minor bugfixes.